package com.wy.demo.jsonFormat.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.*;

public class JsonDemo {
    private Gson gson;
    private User2 testUser2;
    private File jsonFile;

    @Before
    public void setUp() {
        gson = new GsonBuilder()
            .setPrettyPrinting()
            .setDateFormat("yyyy-MM-dd HH:mm:ss")
            .create();
            
        testUser2 = new User2(1L, "张三", "zhangsan@example.com", new Date(), 
            Arrays.asList("读书", "游泳", "编程"));
        jsonFile = new File("src/main/resources/User2.json");
    }

    @Test
    public void testObjectToJsonString() {
        // 对象转 JSON 字符串
        String jsonString = gson.toJson(testUser2);
        System.out.println("对象转 JSON 字符串：");
        System.out.println(jsonString);
        
        assertNotNull(jsonString);
        assertTrue(jsonString.contains("张三"));
        assertTrue(jsonString.contains("email_address"));
    }

    @Test
    public void testJsonStringToObject() {
        // 对象转 JSON 字符串
        String jsonString = gson.toJson(testUser2);
        
        // JSON 字符串转对象
        User2 parsedUser2 = gson.fromJson(jsonString, User2.class);
        System.out.println("\nJSON 字符串转对象：");
        System.out.println("姓名：" + parsedUser2.getName());
        System.out.println("邮箱：" + parsedUser2.getEmail());
        
        assertEquals(testUser2.getName(), parsedUser2.getName());
        assertEquals(testUser2.getEmail(), parsedUser2.getEmail());
    }

    @Test
    public void testObjectToJsonFile() throws IOException {
        // 对象转 JSON 文件
        try (FileWriter writer = new FileWriter(jsonFile)) {
            gson.toJson(testUser2, writer);
        }
        System.out.println("\n对象已写入文件：" + jsonFile.getAbsolutePath());
        
        assertTrue(jsonFile.exists());
        assertTrue(jsonFile.length() > 0);
    }

    @Test
    public void testJsonFileToObject() throws IOException {
        // 从文件读取 JSON 到对象
        try (FileReader reader = new FileReader(jsonFile)) {
            User2 fileUser2 = gson.fromJson(reader, User2.class);
            System.out.println("\n从文件读取 JSON 到对象：");
            System.out.println("姓名：" + fileUser2.getName());
            System.out.println("爱好：" + fileUser2.getHobbies());
            
            assertNotNull(fileUser2);
            assertNotNull(fileUser2.getName());
            assertNotNull(fileUser2.getHobbies());
        }
    }

    @Test
    public void testJsonObjectOperations() {
        // 创建 JsonObject
        JsonObject jsonObject = gson.toJsonTree(testUser2).getAsJsonObject();
        
        System.out.println("\nJsonObject 操作：");
        System.out.println("获取 name 字段：" + jsonObject.get("name").getAsString());
        System.out.println("获取 hobbies 数组：" + jsonObject.get("hobbies"));
        
        assertTrue(jsonObject.has("name"));
        assertTrue(jsonObject.has("hobbies"));
        assertEquals("张三", jsonObject.get("name").getAsString());
    }

    @Test
    public void testJsonArrayOperations() {
        // 创建用户数组
        User2[] User2s = new User2[]{
            new User2(1L, "张三", "zhangsan@example.com", new Date(), Arrays.asList("读书", "游泳")),
            new User2(2L, "李四", "lisi@example.com", new Date(), Arrays.asList("篮球", "足球"))
        };
        
        // 数组转 JSON
        String jsonArray = gson.toJson(User2s);
        System.out.println("\n数组转 JSON：");
        System.out.println(jsonArray);
        
        // JSON 转数组
        Type User2ArrayType = new TypeToken<User2[]>() {}.getType();
        User2[] parsedUser2s = gson.fromJson(jsonArray, User2ArrayType);
        System.out.println("\nJSON 转数组：");
        for (User2 User2 : parsedUser2s) {
            System.out.println("姓名：" + User2.getName());
        }
        
        assertEquals(2, parsedUser2s.length);
        assertEquals("张三", parsedUser2s[0].getName());
        assertEquals("李四", parsedUser2s[1].getName());
    }

    @Test
    public void testCustomTypeAdapter() {
        // 创建自定义的 Gson 实例，使用自定义的 TypeAdapter
        Gson customGson = new GsonBuilder()
            .registerTypeAdapter(Date.class, new DateTypeAdapter())
            .create();
            
        // 测试日期序列化
        String jsonString = customGson.toJson(testUser2);
        System.out.println("\n使用自定义 TypeAdapter 序列化：");
        System.out.println(jsonString);
        
        // 测试日期反序列化
        User2 parsedUser2 = customGson.fromJson(jsonString, User2.class);
        System.out.println("反序列化后的日期：" + parsedUser2.getCreateTime());
        
        assertNotNull(parsedUser2.getCreateTime());
    }
} 